package com.gzlg.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.gzlg.hospital.pojo.Appointment_details;
import com.gzlg.hospital.pojo.Medical_record;
import com.gzlg.hospital.pojo.Patient;
import com.gzlg.hospital.pojo.Registration;
import com.gzlg.hospital.pojo.query.hQuery;
import com.gzlg.hospital.service.PatientService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.Map;

@Controller
public class PatientController {
    @Autowired
    private PatientService patientService;
    //患者列表
    @GetMapping("/patient")
    public String listPatientByName(Model model, hQuery hQuery)
    {
        PageInfo<Patient> patientPageInfo=patientService.listPatientByName(hQuery);
        model.addAttribute("page",patientPageInfo);
        return  "patient";
    }
    //患者个人病历记录
    @GetMapping("/records")
    public String listMedicalRecordByPatientID(Model model,hQuery hQuery, HttpSession session)
    {
        Patient patient=(Patient) session.getAttribute("user");
        PageInfo<Medical_record> medical_recordPageInfo=patientService.getMedicalRecordByPatientID(patient.getId(),hQuery);
        model.addAttribute("page",medical_recordPageInfo);
        return  "Patient/medical_record";
    }
    //患者搜索个人病历记录
    @GetMapping("/recordId")
    public String listMedicalRecordById(Model model,HttpSession session, @RequestParam Integer id, hQuery hQuery)
    {
        Patient patient=(Patient) session.getAttribute("user");
        Map<String, Integer> map = new HashMap<>();
        map.put("id",id);
        map.put("patient_id", patient.getId());
        PageInfo<Medical_record> medical_recordPageInfo=patientService.getMedicalRecordByIdPatientID(map,hQuery);
        model.addAttribute("page",medical_recordPageInfo);
        return  "Patient/medical_record";
    }
    //患者个人简历
    @GetMapping("/resume")
    public String getResumeById(Model model,HttpSession session){
        Patient patient=(Patient) session.getAttribute("user");
        model.addAttribute("patient",patient);
        return "Patient/resume";
    }
    //患者个人主页
    @GetMapping("PatientIndex")
    public String gotoIndex(Model model, HttpSession session) {
        Patient patient = (Patient) session.getAttribute("user");
        if (patient != null) {
            // 查询是否有急诊处理数据
            PageInfo<Appointment_details> appointmentPageInfo = patientService.listAppointmentDetailsByPatientId(patient.getId(), new hQuery());
            model.addAttribute("hasAppointment", appointmentPageInfo.getList() != null && !appointmentPageInfo.getList().isEmpty());
        } else {
            model.addAttribute("hasAppointment", false);
        }
        return "Patient/PatientIndex";
    }

    //患者挂号
    @GetMapping("registration")
    public String registration(){
        return "Patient/registration";
    }
    @PostMapping("addRegistration")
    public String addRegistration(HttpSession session,String symptoms, RedirectAttributes redirectAttributes){
        Patient patient=(Patient) session.getAttribute("user");
        Boolean result=patientService.insertRegistration(patient.getId(),symptoms);
        if(result){
            redirectAttributes.addFlashAttribute("message","挂号成功");
            return "redirect:/registration";
        }else {
            redirectAttributes.addFlashAttribute("message","挂号失败");
            return "redirect:/registration";
        }
    }
    //患者自己的挂号列表
    @GetMapping("listRegistrationByPatientId")
    public String listRegistrationByPatientId(Model model,HttpSession session, hQuery hQuery){
        Patient patient=(Patient) session.getAttribute("user");
        PageInfo<Registration> registrationPageInfo=patientService.listRegistrationById(patient.getId(),hQuery);
        model.addAttribute("page",registrationPageInfo);
        return "Patient/PaRegistration";
    }
    //患者查看某条挂号信息
    @GetMapping("getRegistrationById")
    public String getRegistrationById(Model model,HttpSession  session, @RequestParam Integer  id, hQuery hQuery){
        Map<String, Integer> map = new HashMap<>();
        Patient patient=(Patient) session.getAttribute("user");
        map.put("id",id);
        map.put("patient_id", patient.getId());
        PageInfo<Registration> registrationPageInfo=patientService.getRegistrationByIdPatientId(map,hQuery);
        model.addAttribute("page",registrationPageInfo);
        return "Patient/PaRegistration";
    }
    //患者查看挂号预约信息
    @GetMapping("appointmentDetails")
    public String listAppointmentDetailsByPatientId(Model model,HttpSession session, hQuery hQuery){
        Patient patient=(Patient) session.getAttribute("user");
        PageInfo<Appointment_details> appointment_detailsPageInfo=patientService.listAppointmentDetailsByPatientId(patient.getId(),hQuery);
        model.addAttribute("page",appointment_detailsPageInfo);
        return "Patient/appointment_details";
    }
    //患者查看某条挂号预约信息
    @GetMapping("appointmentDetailsById")
    public String listAppointmentDetailsByIdPatientId(HttpSession  session, Model model,@RequestParam Integer id, hQuery hQuery){
        Patient  patient=(Patient) session.getAttribute("user");
        Map<String, Integer> map = new HashMap<>();
        map.put("id",id);
        map.put("patient_id", patient.getId());
        PageInfo<Appointment_details> appointment_detailsPageInfo=patientService.listAppointmentDetailsByRegistrationIdPatientId(map,hQuery);
        model.addAttribute("page",appointment_detailsPageInfo);
        return "Patient/appointment_details";
    }
    //患者注册
    @PostMapping("insertPatient")
    public String insertPatient(Patient patient, RedirectAttributes redirectAttributes){

        if(patientService.getPatientByAccount(patient.getAccount())==null){
            patientService.insertPatient(patient);
            redirectAttributes.addFlashAttribute("message","注册成功");
            return "redirect:/login";
        }else {
            redirectAttributes.addFlashAttribute("message","注册失败,账号已存在");
            return "redirect:/insertPatient";
        }
    }
    @GetMapping("insertPatient")
    public String insertPatient(){
        return "Patient/insertAccount";
    }
}
